package org.cmodel.plugin2.run;

import com.gitee.starblues.bootstrap.PluginContextHolder;
import com.gitee.starblues.spring.environment.EnvironmentProvider;
import com.gitee.starblues.spring.web.PluginStaticResourceConfig;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {

    @Bean
    public HikariDataSource dataSource(){
        EnvironmentProvider environmentProvider = PluginContextHolder.getEnvironmentProvider();
        // 从主程序获取配置
        String url = environmentProvider.getString("spring.datasource.url");
        String username = environmentProvider.getString("spring.datasource.username");
        String password = environmentProvider.getString("spring.datasource.password");
        String driverClassName = environmentProvider.getString("spring.datasource.driver-class-name");

        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl(url);
        hikariConfig.setUsername(username);
        hikariConfig.setPassword(password);
        hikariConfig.setDriverClassName(driverClassName);

        return new HikariDataSource(hikariConfig);
    }

    @Bean
    public PluginStaticResourceConfig pluginStaticResourceConfig() {
        PluginStaticResourceConfig config = new PluginStaticResourceConfig();
        config.setPathPrefix("custom-prefix");
        return config;
    }
}
